package team3.photoBrowser.utils;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

/**
 * ItemizedOverlay class, 
 * 
 * Can manage a whole set of Overlay objects (the individual items placed on the map)
 * 
 */

@SuppressWarnings("rawtypes")
public class MyItemizedOverlay extends ItemizedOverlay {

	Context mContext;
	// In this array we'll put each of the OverlayItem objects we want on the map.
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	
	public MyItemizedOverlay(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker)); //Center-point at the bottom of the image to be the point at which it's attached to the map coordinates
		mContext = context;
	}

	public void addOverlay(OverlayItem overlay) {
	    mOverlays.add(overlay);
	    populate(); //Will read each of the OverlayItem objects and prepare them to be drawn. Populate() Call to createItem(position).
	}	
	
	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}
	
	@Override
	public int size() {
		return mOverlays.size();
	}
	
	public void clear() {
		mOverlays.clear();
	}
	
	/**
	 *  Set up the ability to handle touch events on the overlay items. 
	 */
	@Override
	protected boolean onTap(int index) {
		
	  OverlayItem item = mOverlays.get(index);
	  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
	  dialog.setTitle(item.getTitle());
	  dialog.setMessage(item.getSnippet());
	  dialog.show();
	  
	  return true;
	}
	
}
